/*
 * @lc app=leetcode.cn id=66 lang=csharp
 *
 * [66] 加一
 */

// @lc code=start
public class Solution {
    public int[] PlusOne(int[] digits) {
        var last9Pos = -1;
        for (int i = digits.Length - 1; i >= 0; i--)
        {
            if (digits[i] != 9)
            {
                last9Pos = i;
                break;
            }
        }
        if (last9Pos == -1)
        {
            var newDigits = new int[digits.Length + 1];
            newDigits[0] = 1;
            return newDigits;
        }
        else
        {
            var newDigits = new int[digits.Length];
            for (int i = 0; i < last9Pos; i++)
            {
                newDigits[i] = digits[i];
            }
            newDigits[last9Pos] = digits[last9Pos] + 1;
            return newDigits;
        }
    }
}
// @lc code=end

